package GUI;

import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.visualization.PickSupport;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;

/**
 * Plugin do obsługi akcji myszy dla GraphPanel
 * @author Piotrek
 */
class GraphPanelPopupMouse extends AbstractPopupGraphMousePlugin implements MouseListener {

    /**
     * Konstruktor pluginu GraphPanelPlugin
     */
    public GraphPanelPopupMouse() {
        // zdarzenia w prawego przycisku myszy
        this(MouseEvent.BUTTON3_MASK);
    }

    private GraphPanelPopupMouse(int modifiers) {
        super(modifiers);
    }

    /**
     * Wyświetla menu po kliknięciu
     * @param e akcja myszki
     */
    protected void handlePopup(MouseEvent e) {
        final VisualizationViewer vv =
                (VisualizationViewer) e.getSource();
        Point2D p = vv.inverseViewTransform(e.getPoint());

        PickSupport pickSupport = vv.getPickSupport();
        if (pickSupport != null) {
            final Vertex v = pickSupport.getVertex(p.getX(), p.getY());
            if (v != null) // kliknięcie na wierzchołek
            {
                JPopupMenu popup = new JPopupMenu();
                popup.add(new AbstractAction("Prawy klik na wierzchołek " + v.toString()) {

                    public void actionPerformed(ActionEvent e) {
                        vv.repaint();
                    }
                });

                popup.show(vv, e.getX(), e.getY());
            } else // kliknięcie na krawędź
            {
                final Edge edge = pickSupport.getEdge(p.getX(), p.getY());
                if (edge != null) {
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(new AbstractAction("Prawy klik na krawędź " + edge.toString()) {

                        public void actionPerformed(ActionEvent e) {
                            System.err.println("masz " + edge);
                        }
                    });
                    popup.show(vv, e.getX(), e.getY());
                }
            }
        }
    }
}
